#include "api_1.h"
#include "uart.h"
#include "spi.h"
#include "log.h"
#include "string.h"
#include "dma.h"
#include "OSAL_Memory.h"
#include "gpio_exit.h"
//Ҫ��ʾ�ĵƵ�����
uint8_t led_num = tx1812_NUM_LEDS;

hal_spi_t spiled_spi1 =
{
    .spi_index = SPI1,
};

hal_spi_t spiled_spi0 =
{
    .spi_index = SPI0,
};

void spi1_cb(spi_evt_t* evt)
{
}
void spi0_cb(spi_evt_t* evt)
{
}

spi_Cfg_t spi1_cfg = 
{
	  .sclk_pin = GPIO_DUMMY,
    .ssn_pin = GPIO_DUMMY,
    .MOSI = GPIO_P34,
    .MISO = GPIO_DUMMY,

    .baudrate = 5000000,//9142857-9142858ֱ�����䣬4000000
    .spi_tmod = SPI_TRXD,
    .spi_scmod = SPI_MODE0,
    .spi_dfsmod = SPI_1BYTE,

    #if DMAC_USE
    .dma_tx_enable = false,
    .dma_rx_enable = false,
    #endif

    .int_mode = false,
    .force_cs = true,
    .evt_handler = spi1_cb,
};

spi_Cfg_t spi0_cfg = 
{
	  .sclk_pin = GPIO_DUMMY,
    .ssn_pin = GPIO_DUMMY,
    .MOSI = GPIO_P02,
    .MISO = GPIO_DUMMY,

    .baudrate = 5000000,//9142857-9142858ֱ�����䣬4000000
    .spi_tmod = SPI_TRXD,
    .spi_scmod = SPI_MODE0,
    .spi_dfsmod = SPI_1BYTE,

    #if DMAC_USE
    .dma_tx_enable = false,
    .dma_rx_enable = false,
    #endif

    .int_mode = false,
    .force_cs = true,
    .evt_handler = spi0_cb,
};


//static DMA_CH_CFG_t cfg0;
void dma0_cb(DMA_CH_t ch)
{
    LOG("\ndma done!!!\n");
}

void dma1_cb(DMA_CH_t ch)
{
    LOG("\ndma done!!!\n");
}
void DMA_Demo_Init(void)
{
	hal_gpio_init();
	hal_dma_init();
	HAL_DMA_t ch_cfg;
	ch_cfg.dma_channel = DMA_CH_0;
	ch_cfg.evt_handler = dma0_cb;
	hal_dma_init_channel(ch_cfg);
	
	
	ch_cfg.dma_channel = DMA_CH_1;
	ch_cfg.evt_handler = dma1_cb;
	hal_dma_init_channel(ch_cfg);
	LOG("dma demo start...\n");
	
}
uint8_t tx1812_buffer[tx1812_BUFFER_SIZE] = {0};		
int API0_1_Init(void)
{
	uint8_t retval = PPlus_SUCCESS;
	hal_spi_init(SPI1);
	hal_spi_init(SPI0);
	retval = hal_spi_bus_init(&spiled_spi1,spi1_cfg);
	
	if(retval != PPlus_SUCCESS)
	{
			LOG("spi init err!please check it!\n");
			return retval;		
	}
	
	retval = hal_spi_bus_init(&spiled_spi0,spi0_cfg);
	
	if(retval != PPlus_SUCCESS)
	{
			LOG("spi init err!please check it!\n");
			return retval;		
	}
	DMA_Demo_Init();
	
//	tx1812_buffer = (uint8_t *)osal_mem_alloc(tx1812_BUFFER_SIZE);
	return retval;		
}






void tx1812_send_spi(void) //右
{
	hal_spi_dma_set(&spiled_spi1,1,0);
//	LOG("%x",tx1812_buffer[0]);
//	LOG("%x",tx1812_buffer[1]);
//	LOG("%x",tx1812_buffer[2]);
//	LOG("%x",tx1812_buffer[3]);
//	LOG("%x",tx1812_buffer[4]);
//	LOG("%x",tx1812_buffer[5]);
//	LOG("%x",tx1812_buffer[6]);
//	LOG("%x",tx1812_buffer[7]);
//	spi_tx_buf(&spiled_spi1,tx1812_buffer,tx1812_BUFFER_SIZE);
	hal_spi_transmit(&spiled_spi1,SPI_TXD,tx1812_buffer,NULL,tx1812_BUFFER_SIZE,0);

}

//void tx1812_send_spi_lrl(void)//右 
//{
//	hal_spi_dma_set(&spiled_spi1,1,0);
//	hal_spi_transmit(&spiled_spi1,SPI_TXD,lrl_data,NULL,HEADLIGHT * 24 + 40,0);

//}

//void tx1812_send_spi0_lrl(void) //左
//{
//	hal_spi_dma_set(&spiled_spi0,1,0);
//	hal_spi_transmit(&spiled_spi0,SPI_TXD,lrl_data,NULL,HEADLIGHT * 24 + 40 ,0);

//}

void tx1812_send_spi0(void) //左
{
	hal_spi_dma_set(&spiled_spi0,1,0); 
//	LOG("%x",tx1812_buffer[0]);
//	LOG("%x",tx1812_buffer[1]);
//	LOG("%x",tx1812_buffer[2]);
//	LOG("%x",tx1812_buffer[3]);
//	LOG("%x",tx1812_buffer[4]);
//	LOG("%x",tx1812_buffer[5]);
//	LOG("%x",tx1812_buffer[6]);
//	LOG("%x",tx1812_buffer[7]);
//	spi_tx_buf(&spiled_spi1,tx1812_buffer,tx1812_BUFFER_SIZE);
	hal_spi_transmit(&spiled_spi0,SPI_TXD,tx1812_buffer,NULL,tx1812_BUFFER_SIZE,0);
}
